﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DataAccessLayer;
using Entities;
using BusinessLogicLayer;
using BusinessLogicLayer.Validation;
using System.Text.RegularExpressions;
namespace QuanLyTKB_TienDoGD.QuanLyGiaoVien
{
    public partial class frmAddGiaoVien : Office2007Form
    {
        private GiaoVienManager _manager;
        private GiaoVien _giaovien;
        private bool _isUpdate;
        frmQuanLyGiaoVien frmQLGV;
        public frmAddGiaoVien()
        {
            InitializeComponent();
        }
        public frmAddGiaoVien(GiaoVienManager _man, frmQuanLyGiaoVien p, GiaoVien _mh = null)
            : this()
        {
            _manager = _man;
            _giaovien = _mh;
            frmQLGV = p;
            _isUpdate = (_mh != null);
        }
        private void frmAddGiaoVien_Load(object sender, EventArgs e)
        {
            if (_isUpdate)
            {
                txtMaGV.Text = _giaovien.MaGV;
                txtTenGV.Text = _giaovien.TenGV;
                dtNgaySinh.Value = _giaovien.NgaySinh;
                if (rdNam.Checked)
                    rdNam.Text = _giaovien.GioiTinh;
                else
                    rdNu.Text = _giaovien.GioiTinh;
                txtChucvu.Text = _giaovien.ChucVu;
                txtHocvi.Text = _giaovien.HocVi;
                txtDiachi.Text = _giaovien.DiaChi;
                txtDienthoai.Text = _giaovien.DienThoai;
                txtEmail.Text = _giaovien.Email;
            }
            txtMaGV.ReadOnly = _isUpdate;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            _giaovien = new GiaoVien();

            _giaovien.MaGV = txtMaGV.Text;
            _giaovien.TenGV = txtTenGV.Text;
            _giaovien.NgaySinh = dtNgaySinh.Value;
            if (rdNam.Checked)
                _giaovien.GioiTinh = rdNam.Text;
            else
                _giaovien.GioiTinh = rdNu.Text;
            //_giaovien.GioiTinh = "Nam";
            _giaovien.ChucVu = txtChucvu.Text;
            _giaovien.HocVi = txtHocvi.Text;
            _giaovien.DiaChi = txtDiachi.Text;
            _giaovien.DienThoai = txtDienthoai.Text;
            _giaovien.Email = txtEmail.Text;

            var vResult = _manager.Validate(_giaovien);
            if (vResult.Valid)
            {
                var aResult = _manager.InsertOrUpdate(_giaovien);
                if (aResult.Success)
                {
                    try
                    {
                        frmQLGV.btnLamMoi.PerformClick();
                    }
                    catch (Exception)
                    {

                    }
                    MessageBox.Show((_isUpdate ? "Cập nhật" : "Thêm") + "thành công thông tin giáo viên", "Thông báo");
                    btnReset.PerformClick();

                }
                else
                    MessageBox.Show(aResult.GetErrosMessages());
            }
            else MessageBox.Show(vResult.ToString());
        }

        private void dtNgaySinh_Click(object sender, EventArgs e)
        {

        }

        private void groupPanel1_Click(object sender, EventArgs e)
        {

        }

        private void txtDienthoai_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            this.txtMaGV.Text = "";
            this.txtTenGV.Text = "";
            this.txtChucvu.Text = "";
            this.txtHocvi.Text = "";
            this.txtDiachi.Text = "";
            this.txtDienthoai.Text = "";
            this.txtEmail.Text = "";

        }

        private void txtEmail_TextChanged(object sender, EventArgs e)
        {
            string pat = @"(\w+)@(gmail)\.com$";
            if (Regex.IsMatch(this.txtEmail.Text, pat))
            {
                this.errorProvider1.SetError(txtEmail, "");
            }
            else
                this.errorProvider1.SetError(txtEmail, "Bạn đã nhập sai email!");
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
